var loaderUtils = require('loader-utils');
var htmlLoad = require('html-loader');
var _ = require('lodash');
var Engine = require('velocity').Engine;
var mockData = require('../../app/lib/mockData');
var path = require('path');

module.exports = function(source) {
    this.cacheable && this.cacheable();
    var callback = this.async();
    var query = loaderUtils.parseQuery(this.query);
    if(_.isEmpty(query)){
        callback(null,htmlLoad(source));
    }else{
        var engine = new Engine({template: source, root: query.vmDir});
        mockData.getMockData(query.fileVmPath,path.basename(query.fileVmPath, '.vm')).then(function (body) {
            var data = JSON.stringify(body);
            var result = engine.render(JSON.parse(data));
            callback(null,htmlLoad(result))
        })
    }

};